﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HowItWentDown.Enemies
{
    class ShadowMonster : Enemy
    {
        bool DEBUG = false;
        float floatSpeed; // number of ms in between full rotations (i.e. the period of the sin wave)
        float runningTimer; // keeps track of how much time has passed during the current rotation
        float finalY; // what the end position of our monster should be
        float shootTimer = 0;
        public ShadowMonster(Game1 doc, float newX, float newY, float newZ, Texture2D tex, float speed, float finalHeight)
            : base(doc, newX, newY, newZ, tex)
        {
            floatSpeed = speed;
            finalY = finalHeight;
            this.floating = true;
            scale = 0.1f;
            rect.CenterOrigin();
            health = 10;
        }

        /// <summary>
        /// Updates the rotation of the monster.
        /// </summary>
        /// <param name="dt">Difference in time between this frame and last frame, in ms.</param>
        public override void Update()
        {
            // Changed this method to not take in a float for the amount of passed time, since the method won't be recognized without type casting, which would be slow
            //runningTimer += dt;
            runningTimer += Global.Speed;
            float heightAdj = (float)Math.Sin((double)runningTimer / floatSpeed) * finalY;
            //if (runningTimer > floatSpeed) runningTimer = 0;
            this.pos.Y += heightAdj;
            //if (DEBUG) Console.WriteLine("dt: " + dt + "heightAdj: " + heightAdj);

            // shoot at the player if he is closer than 200 units to the monster
            if (MyMath.Distance(pos, Global.Player.pos) < 100)
            {
                // the shoot timer is increased by the speed of the game
                shootTimer += Global.Speed;
                // if the timer is more than 240 (2 seconds, or 240 frames at 60FPS), the monster shoots
                if (shootTimer > 240)
                {
                    shootTimer -= 240;
                    var ball = new ShadowBall(doc, x, y, z, 1.5f, Global.Player.pos + new Vector3(0, 20, 0));
                }
            }
            base.Update();
        }
    }
    public class ShadowBall : Obj
    {
        float timer;
        float smoketimer;
        public ShadowBall(Game1 Doc, float X, float Y, float Z, float speed, Vector3 Target)
            :base(Doc,X,Y,Z,Global.LoadTexture("Rock"),false)
        {
            scale = 0.07f;
            velocity = Extensions.Normalize(Target - pos, speed);
            collision = true;
            rect.CenterOrigin();
            visible = false;
        }
        public override void Update()
        {
            smoketimer += Global.Speed;
            if (smoketimer > 3)
            {
                smoketimer -= 3;
                var s = new Disappear(doc, x, y, z, Global.LoadTexture("Smoke")) { scalespeed = MyMath.RandomRange(0.05f,0.1f), maxAlpha = 0.6f };
                s.rotspeed.Z = MyMath.RandomRange(-1f, 1f);
                s.rect.CenterOrigin();
                s.Color = new Color(100, 100, 100);
            }
            if (y < 0)
            {
                remove();
                for (int i = 0; i < 20; i++)
                {
                    var s = new Disappear(doc, x, y, z, Global.LoadTexture("Smoke")) { scale = 0.1f, maxFrames = 60 };
                    s.velocity = new Vector3(MyMath.RandomRange(-2, 2), MyMath.RandomRange(-2, 2), MyMath.RandomRange(-2, 2));
                    s.Color = Color.Yellow;
                    s.gravity = 0.1f;
                }
            }
            timer += Global.Speed;
            if (timer > 300)
                remove();
            
            base.Update();
        }
    }
}
